home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Magazin/MacEasy 14
/
Mac Magazin and MacEasy Magazine CD - Issue 14.iso
/
Wissenschaft & Technik
/
Tools Plus 2.6.1 Evaluation Kit
/
Tools Plus 2.6.1
/
Tutorials
/
3-List Boxes
/
Tutorial.p
< prev
Wrap
Text File
|
1995-08-25
|
7KB
|
218 lines
{ Tools Plus Tutorial - - List Boxes }
{ NOTE: }
{ The MWERKS compiler directived ($IFC MWERKS) indicates code that is compiled only by }
{ the CodeWarrior compiler. THINK Pascal and Metrowerks CodeWarrior Pascal have }
{ _slight_ differences, and this compiler directive lets you use one source for both compilers. }
{ You can remove the code that does not pertain to your compiler. }
program Tutorial;
uses
{$IFC MWERKS}
Dialogs, Fonts, Resources, Processes, SegLoad, TextEdit, ToolsPlus, Windows;
{$ELSEC}
ToolsPlus;
{$ENDC}
const
{Constants to make code more readable…}
LeftList = 1;
RightList = 2;
MoveButton = 1;
RemoveButton = 2;
DoneButton = 255;
LeftToRight = 1;
RightToLeft = -1;
var
Poll: TPPollRecord; {Polling record to retrieve event information}
ExitTheDemo: boolean; {Should the demo terminate?}
Direction: integer; {Direction in which lines are moved (left-to-right or right-to-left) }
{ - - - - - - - - - - - - - - - - - - - - - - - - - - - }
procedure ApplicationInitialization;
const
DemoWindow1 = 1;
var
Font: FontInfo; {Info about current font }
BoxHeight: integer; {List box's height in pixels }
resCount: integer; {Number of resources }
resIndex: integer; {Resource index counter }
hRsrc: handle; {Handle to a resource }
theResID: integer; {Resource's ID }
theResType: ResType; {Resource's Type }
theResName: Str255; {Resource's Name }
InsertLineAt: integer; {Location where line should be inserted in list to be in alphabetic order }
begin
{ Do all the application setup before you start polling for events… }
WindowOpen(DemoWindow1, 0, 0, 440, 130, '', dBoxProc + wCenter, NoGoAway, Modal);
{ Create left list box, making it exactly 6 lines high using Chicago 12pt…}
TextFont(systemFont);
TextSize(12);
GetFontInfo(Font);
BoxHeight := 6 * (Font.ascent + Font.descent + Font.leading);
NewListBox(LeftList, 15, 15, 145, 15 + BoxHeight, lOnlyOne);
{ Create left list box, making it exactly 8 lines high using Geneva 9pt…}
TextFont(geneva);
TextSize(9);
GetFontInfo(Font);
BoxHeight := 8 * (Font.ascent + Font.descent + Font.leading);
NewListBox(RightList, 280, 15, 410, 15 + BoxHeight, lOnlyOne);
{ Create the buttons used to control these lists… }
NewButton(MoveButton, 175, 20, 265, 40, 'Move', pushButProc, disabled, notSelected);
NewButton(RemoveButton, 175, 50, 265, 70, 'Remove', pushButProc, disabled, notSelected);
NewButton(DoneButton, 185, 92, 255, 112, 'Done', pushButProc, enabled, notSelected);
{ Populate the left list with the names of all the fonts. We are assuming that you are using System 6 or }
{ later, which uses 'FOND' resourses for fonts.}
DrawListBox(LeftList, false); {Stop list box drawing for faster speed}
SetResLoad(false); {Don't load resources for more speed}
resCount := CountResources('FOND'); {How many fonts are there?}
for resIndex := 1 to resCount do
begin
hRsrc := GetIndResource('FOND', resIndex); {Get a handle to the font}
GetResInfo(hRsrc, theResID, theResType, theResName); {Get the font's ID, Type and Name }
InsertLineAt := SearchListBox(LeftList, theResName); {Where should this name go?}
InsertListBoxLine(LeftList, InsertLineAt); {Insert an empty line}
SetListBoxText(LeftList, InsertLineAt, theResName); {Put font name into empty line}
end;
SetResLoad(true); {Resume loading resources}
DrawListBox(LeftList, true); {Draw the list box's contents}
Direction := none;
ExitTheDemo := false;
end;
{ - - - - - - - - - - - - - - - - - - - - - - - - - - - }
procedure ActionInWindow;
var
SourceList, TargetList: integer; {Source and target list when moving lines}
SourceLine, TargetLine: integer; {Line number in source and target list}
FontName: Str255;
begin
CurrentWindow(1); {Subsequent actions affect window #1 (our only window) }
case Poll.What of
doListBox:
if GetListBoxLines(Poll.ListBox.Num, 1) = none then {If an empty line was clicked…}
begin {Nothing selected, so disable the buttons and deselect lines in both lists…}
Direction := none;
ButtonTitle(MoveButton, 'Move');
EnableButton(MoveButton, off);
EnableButton(RemoveButton, off);
SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
end
else
begin {If a non-empty line was clicked in the list box…}
case Poll.ListBox.Num of
LeftList:
begin
Direction := LeftToRight;
ButtonTitle(MoveButton, '>> Move >>');
{ Turn off the line in the other list (if it has one selected)… }
SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
end;
RightList:
begin
Direction := RightToLeft;
ButtonTitle(MoveButton, '<< Move <<');
{ Turn off the line in the other list (if it has one selected)… }
SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
end
end;
EnableButton(MoveButton, on);
EnableButton(RemoveButton, on);
end;
doButton:
case Poll.Button.Num of
MoveButton: {Move line from source list to target list…}
begin
if Direction = LeftToRight then
begin
SourceList := LeftList;
TargetList := RightList;
ButtonTitle(MoveButton, '<< Move <<');
end
else
begin
SourceList := RightList;
TargetList := LeftList;
ButtonTitle(MoveButton, '>> Move >>');
end;
SourceLine := GetListBoxLines(SourceList, 1); {Find selected line in source list}
GetListBoxText(SourceList, SourceLine, FontName); {Get selected line's text}
DeleteListBoxLine(SourceList, SourceLine); {Delete line in source list}
TargetLine := SearchListBox(TargetList, FontName); {Where should this name go in target list?}
InsertListBoxLine(TargetList, TargetLine); {Insert an empty line}
SetListBoxText(TargetList, TargetLine, FontName); {Put font name into empty line}
SetListBoxLine(TargetList, TargetLine, on); {Select line in target list}
Direction := -Direction; {Change direction of movement}
end;
RemoveButton: {Remove the selected line…}
begin
if Direction = LeftToRight then
SourceList := LeftList
else
SourceList := RightList;
DeleteListBoxLine(SourceList, GetListBoxLines(SourceList, 1));
Direction := none;
ButtonTitle(MoveButton, 'Move');
EnableButton(MoveButton, off);
EnableButton(RemoveButton, off);
end;
DoneButton: {We're done, quit the application…}
ExitTheDemo := true;
end
end
end;
{ - - - - - - - - - - - - - - - - - - - - - - - - - - - }
begin
{$IFC MWERKS}
{Toolbox initialization - - Done automatically by THINK Pascal}
InitGraf(@qd.thePort);
InitFonts;
InitWindows;
InitMenus;
TEInit;
InitDialogs(nil);
MaxApplZone;
{$ENDC}
if not InitToolsPlus(0, 1, UseColor) then
ExitToShell;
ApplicationInitialization;
while not ExitTheDemo do {Main Event Loop}
if PollSystem(Poll) then {If an event is available, process the event…}
if (Poll.What = doListBox) or (Poll.What = doButton) then {User clicked something in this window}
ActionInWindow;
{All other events are ignored}
end.